//
//  GHSeleTabCell.m
//  VideoPic
//
//  Created by GH on 16/3/26.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "GHSeleTabCell.h"
#import "GHSeleModel.h"
#import <YYWebImage.h>


@interface GHSeleTabCell ()
@property (nonatomic ,retain)UIImageView *imageHead;
@property (nonatomic ,retain)UILabel *labelOfname;
@property (nonatomic ,retain)UILabel *labelContect;
@property (nonatomic ,retain)UILabel *labelOftime;

@end

@implementation GHSeleTabCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {

    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        [self createLabel];
        [self createImage];
    }
    return self;

}
- (void)createImage {
    self.imageHead = [[UIImageView alloc] init];
    [self.contentView addSubview:self.imageHead];

}

- (void)createLabel {

    self.labelOfname = [[UILabel alloc] init];
    [self.contentView addSubview:self.labelOfname];
    
    self.labelContect = [[UILabel alloc] init];
    [self.contentView addSubview:self.labelContect];
    
    self.labelOftime = [[UILabel alloc] init];
    [self.contentView addSubview:self.labelOftime];


}

- (void)passWithModelForNew:(GHSeleModel *)model {

    if ([model.userUrl isEqual:[NSNull null]]) {
        
        NSLog(@"空照片");
    }else{
        self.imageHead.yy_imageURL = [NSURL URLWithString:model.user_profile_image_url];
        
    }
    self.labelOfname.text = model.user_name;
    self.labelContect.text = model.text;
    NSString *str = model.create_time;
    
    NSTimeInterval time=[str doubleValue];
    NSDate *detaildate=[NSDate dateWithTimeIntervalSince1970:time];
    //实例化一个NSDateFormatter对象
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //设定时间格式,这里可以设置成自己需要的格式
    [dateFormatter setDateFormat:@"MM-dd HH:mm"];
    
    NSString *currentDateStr = [dateFormatter stringFromDate: detaildate];
    
    self.labelOftime.text = currentDateStr;
    




}

- (void)passWithModel:(GHSeleModel *)model {
    if ([model.userUrl isEqual:[NSNull null]]) {
        
        NSLog(@"空照片");
    }else{
        self.imageHead.yy_imageURL = [NSURL URLWithString:model.userUrl];
    
    }
    self.labelOfname.text = model.userName;
    self.labelContect.text = model.textcon;

    NSString *str = model.createTime;
    
    NSTimeInterval time=[str doubleValue];
    NSDate *detaildate=[NSDate dateWithTimeIntervalSince1970:time];
    //实例化一个NSDateFormatter对象
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //设定时间格式,这里可以设置成自己需要的格式
    [dateFormatter setDateFormat:@"MM-dd HH:mm"];
    
    NSString *currentDateStr = [dateFormatter stringFromDate: detaildate];

    self.labelOftime.text = currentDateStr;
    
    
}




- (void)layoutSubviews {
    [super layoutSubviews];

    self.imageHead.frame = CGRectMake(20, 20, SCREEN_WIDTH /10, SCREEN_WIDTH / 10);
    self.imageHead.layer.masksToBounds = YES;
    self.imageHead.layer.cornerRadius = SCREEN_WIDTH / 20;
    
    self.labelOfname.frame = CGRectMake(30 + SCREEN_WIDTH / 10, 20 , SCREEN_WIDTH/2, 25);
    self.labelOfname.font = [UIFont systemFontOfSize:13];
    self.labelOfname.textColor = [UIColor grayColor];

    
    self.labelContect.frame = CGRectMake(20,  20 + SCREEN_WIDTH /10 +10, SCREEN_WIDTH - 40, [self heightForLabel:self.labelContect.text]);
    self.labelContect.numberOfLines = 0;
    self.labelContect.font = [UIFont systemFontOfSize:15];
    self.labelContect.textColor = [UIColor grayColor];
    
    
    self.labelOftime.frame = CGRectMake(30 +SCREEN_WIDTH /10, 40, 100, 25);
    self.labelOftime.textColor = [UIColor grayColor];
    self.labelOftime.font = [UIFont systemFontOfSize:10];
}


-(CGFloat)heightForLabel:(NSString *)text{
    
    CGRect rect = [text boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width - 40, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:17]} context:nil];
    
    
    return rect.size.height;
}

+(CGFloat)heightForLabel:(NSString *)text{
    
    CGRect rect = [text boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width - 40, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:17]} context:nil];
    
    
    return rect.size.height;
}

+ (CGFloat)heightForcellWith:(GHSeleModel *)model {

    CGFloat height;

    height = 20 + [UIScreen mainScreen].bounds.size.width /10 +10 + [[self class] heightForLabel:model.textcon] + 20;
    
    return height;
}

+ (CGFloat)heightForcellWithTop:(GHSeleModel *)model {
    
    CGFloat height;
    
    height = 20 + [UIScreen mainScreen].bounds.size.width /10 +10 + [[self class] heightForLabel:model.text] + 20;
    
    return height;
}



- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
